package com.yanyu.space.common.db.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;

import java.util.Date;

/**
 * 自动装配
 *
 * @author yanyu
 */
public class MetaObjectHandlerConfig implements MetaObjectHandler {

    private static final String createUserNameInBean = "createUser";
    private static final String createTimeNameInBean = "createTime";
    private static final String updateUserNameInBean = "updateUser";
    private static final String updateTimeNameInBean = "updateTime";
    private static final Long userId = 1L;

    @Override
    public void insertFill(MetaObject metaObject) {

        boolean createUser = metaObject.hasSetter(createUserNameInBean);
        if (createUser) {
            this.strictInsertFill(metaObject, createUserNameInBean, Long.class, userId);
        }
        boolean createTime = metaObject.hasSetter(createTimeNameInBean);
        if (createTime) {
            this.strictInsertFill(metaObject, createTimeNameInBean, Date.class, new Date());
        }
        boolean updateUser = metaObject.hasSetter(updateUserNameInBean);
        if (updateUser) {
            this.strictInsertFill(metaObject, updateUserNameInBean, Long.class, userId);
        }
        boolean updateTime = metaObject.hasSetter(updateTimeNameInBean);
        if (updateTime) {
            this.strictInsertFill(metaObject, updateTimeNameInBean, Date.class, new Date());
        }
    }

    @Override
    public void updateFill(MetaObject metaObject) {

        boolean updateUser = metaObject.hasSetter(updateUserNameInBean);
        if (updateUser) {
            this.strictInsertFill(metaObject, updateUserNameInBean, Long.class, userId);
        }
        boolean updateTime = metaObject.hasSetter(updateTimeNameInBean);
        if (updateTime) {
            this.strictInsertFill(metaObject, updateTimeNameInBean, Date.class, new Date());
        }
    }
}
